/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.dbtools.datatypes;

import java.util.*;

import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.agentwise.*;

/**
 * This class is an structure that has all the information that represents a Table.<br>
 * Can be used to create a new table or to modify an existing one.
 *
 * @author Francisco Morero Peyrona
 */

public final class TableDefinition extends DbEntityDefinition implements Cloneable
{
   private Vector vColumns;    // Columns (fields)
   private Vector vIndices;    // Other indexes
   private Vector vRelations;  // Relations (Relational DB info)
   
   //-------------------------------------------------------------------------//

   /**
    * Class constructor
    * 
    * @param td An instance of <code>DbEntityDescriptor</code>
    */
   public TableDefinition( DbEntityDescriptor dedTable )
   {
      super( dedTable );
      
      setName( dedTable.getName() );   // Changes null (if is the case) by "NoName"
      
      this.vColumns   = new Vector();
      this.vIndices   = new Vector();
      this.vRelations = new Vector();
      
      AgentMetaData agent = DbUtil.getAgentDBMS().getAgentMetaData();
      
      setColumns( agent.getTableColumns( dedTable ) );
      setIndices( agent.getTableIndices( dedTable ) );
      setRelations( agent.getTableImportedRelations( dedTable ) );
   }
   
   /**
    * 
    * @param sName
    */
   public void setName( String sName )
   {
      if( sName == null )
         sName = "NoName";
      
      this.ded.setName( sName );
   }
   
   /**
    * 
    * @return
    */
   public DbEntityDescriptor getDbEntityDescriptor()
   {
      return this.ded;
   }
   
   /**
    * @see java.Object#clone()
    */
   public Object clone()
   {
      TableDefinition o = (TableDefinition) super.clone();
                      o.vColumns   = (Vector) this.vColumns.clone();
                      o.vIndices   = (Vector) this.vIndices.clone();
                      o.vRelations = (Vector) this.vRelations.clone();
      return o;
   }
   
   //-------------------------------------------------------------------------//
   // COLUMNS (fields) RELATED METHODS
   //-------------------------------------------------------------------------//
   
   /**
    * 
    * @param column
    */
   public void addColumn( TableColumnDefinition column )
   {
      this.vColumns.add( column );
   }

   /**
    * 
    * @param column
    */
   public void removeColumn( TableColumnDefinition column )
   {
      this.vColumns.remove( column );
   }

   /**
    * 
    * @return
    */
   // From internal Vector to array
   public TableColumnDefinition[] getColumns()
   {
      TableColumnDefinition[] aRet = new TableColumnDefinition[ this.vColumns.size() ];
      this.vColumns.copyInto( aRet );

      return aRet;
   }

   // From array to internal Vector
   public void setColumns( TableColumnDefinition[] aColumn )
   {
      this.vColumns.removeAllElements();

      for( int n = 0; n < aColumn.length; n++ )
         this.vColumns.add( aColumn[n] );
   }

   //-------------------------------------------------------------------------//
   // INDICES RELATED METHODS
   //-------------------------------------------------------------------------//
   public void addIndex( IndexDefinition index )
   {
      this.vIndices.add( index );
   }

   public void removeIndex( IndexDefinition index )
   {
      this.vIndices.remove( index );
   }

   // From internal Vector to array
   public IndexDefinition[] getIndices()
   {
      IndexDefinition[] aRet = new IndexDefinition[ this.vIndices.size() ];
      this.vIndices.copyInto( aRet );

      return aRet;
   }

   // From array to internal Vector
   public void setIndices( IndexDefinition[]  aIndex )
   {
      this.vIndices.removeAllElements();

      for( int n = 0; n < aIndex.length; n++ )
         this.vIndices.add( aIndex[n] );
   }

   //-------------------------------------------------------------------------//
   // RELATIONS RELATED METHODS
   //-------------------------------------------------------------------------//
   public void addRelation( RelationDefinition relation )
   {
      this.vRelations.add( relation );
   }

   public void removeRelation( RelationDefinition relation )
   {
      this.vRelations.remove( relation );
   }

   // From internal Vector to array
   public RelationDefinition[] getRelations()
   {
      RelationDefinition[] aRet = new RelationDefinition[ this.vRelations.size() ];
      this.vRelations.copyInto( aRet );

      return aRet;
   }

   // From array to internal Vector
   public void setRelations( RelationDefinition[]  aRelation )
   {
       this.vRelations.removeAllElements();

      for( int n = 0; n < aRelation.length; n++ )
         this.vRelations.add( aRelation[n] );
   }
}